package com.binguo.wxapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.binguo.common.util.ImageBase64Util;
import com.binguo.common.util.JsonUtil;
import com.binguo.common.util.PriceUtil;
import com.binguo.model.mapper.CashierDeskMapper;
import com.binguo.model.mapper.LogProductImgFromUserMapper;
import com.binguo.model.model.CashierDesk;
import com.binguo.model.model.LogProductImgFromUser;
import com.binguo.wxapi.contant.AppConstant;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.result.ResultGenerator;
import com.binguo.wxapi.vo.product.ProductRecResultVO;
import com.binguo.wxapi.vo.product.ProductRecResultVONewWithLocation;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RestController
@RequestMapping
public class TestController {

    public static void main(String[] args) {
        String a = "94.5";

        System.out.println(PriceUtil.getRealPrice(a));

        String s = "{\"traceId\":\"1332475d44d17cf1\",\"code\":\"10000\",\"data\":\"{\\\"amount\\\":\\\"3.00\\\",\\\"balance\\\":\\\"7.00\\\",\\\"code\\\":\\\"2\\\",\\\"orderDiscountAmount\\\":\\\"0.00\\\",\\\"orderId\\\":\\\"2007140122226979\\\"}\",\"method\":\"api.pay.order.settlement.create\",\"requestId\":\"1594696037315\",\"sign\":\"4be2d2d16ade6e8ff4f88d7cf5bd912d\",\"supplierCode\":\"13333400556\",\"message\":\"成功\",\"version\":\"1.0\",\"timestamp\":\"1594696023371\"}";

        JSONObject  jsonObject = JSONObject.parseObject(s);
        Map<String,Object> map = (Map<String,Object>)jsonObject;//

        System.out.println(map);
        System.out.println(map.get("data"));

        JSONObject  jsonObject2 = JSONObject.parseObject((String)map.get("data"));
        Map<String,Object> map2 = (Map<String,Object>)jsonObject2;//

        System.out.println(map2.get("balance"));
    }

    @Resource
    private CashierDeskMapper cashierDeskMapper;


    /**
     * 校验识别
     *
     * @return
     */
    @PostMapping("/api/wxapp/productVerify/{shopId}")
    public Result productVerify(@RequestParam("file") MultipartFile file,
                              @PathVariable("shopId")Integer shopId) throws Exception{
        String filepath = AppConstant.imgPath + "/productVerify/";
        File destFile = new File(filepath);
        if (!destFile.exists()) {
            destFile.mkdir();
        }
        filepath += shopId;
        destFile = new File(filepath);
        if (!destFile.exists()) {
            destFile.mkdir();
        }
        filepath += "/" + System.currentTimeMillis() + ".png";

        File toFile = new File(filepath);
        file.transferTo(toFile);
        String base64 = ImageBase64Util.getImageBinary(filepath);

        Condition condition = new Condition(CashierDesk.class);
        condition.setOrderByClause(" id desc limit 1");
        condition.or().andEqualTo("shopid", shopId)
                .andEqualTo("deleted", false)
        ;
        List<CashierDesk> cashierDeskList = cashierDeskMapper.selectByCondition(condition);
        if (cashierDeskList == null && cashierDeskList.size() == 0) {
            return ResultGenerator.genFailResult("");
        }

        Map<String, String> map = new HashMap<>();
        map.put("fileContent", base64);
        map.put("shopId", shopId.toString());
        map.put("deviceId", cashierDeskList.get(0).getCcode());
        map.put("type", "1");
        String jsonParam = JsonUtil.toJson(map);

        HttpHeaders DEFAULT_JSON_HEADER_UTF8 = new HttpHeaders();
        DEFAULT_JSON_HEADER_UTF8.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(jsonParam, DEFAULT_JSON_HEADER_UTF8);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity("http://172.17.0.5/api/product/recWithLocation", entity, String.class);

        String body = responseEntity.getBody();

        Result productRecResultVO = JsonUtil.parse(body, Result.class);

        toFile.delete();
        if (productRecResultVO.getCode() == 200) {

            LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap)productRecResultVO.getData();

            String resutlUrl = (String)linkedHashMap.get("imgUrl");

            return ResultGenerator.genSuccessResult(resutlUrl);
        }

        return ResultGenerator.genFailResult("");

    }

}
